package com.example.service;

import com.example.entity.StudentScore;
import com.example.mapper.StudentScoreRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class StudentScoreService {
    @Autowired
    private StudentScoreRepository repository;

    public StudentScore saveScore(StudentScore score) throws Exception {
        if (repository.existsByName(score.getName())) {
            throw new Exception("A student with the same name already exists.");
        }
        return repository.save(score);
    }

    // 获取特定学校和专业的学生分数，并按分数降序排列
    public List<StudentScore> getScoresBySchoolAndMajor(String school, String major) {
        return repository.findBySchoolAndMajorOrderByScoreDesc(school, major);
    }
}

